﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    /// <summary>
    /// 灾情管理
    /// </summary>
    public class DisasterLevelRepository: BaseRepository<TDisasterLevel>, IDisasterLevelRepository
    {
        public DisasterLevelRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddDisasterLevel(DisasterLevelDto dto)
        {
            //事务
            using(var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    TDisasterLevel disaster = new TDisasterLevel()
                    {
                        Name = dto.Name,
                        Type = dto.Type,
                        Season = dto.Season,
                        Level = dto.Level,
                        Remark = dto.Remark,
                        CreateDate = DateTime.Now,
                        CreateUserId = dto.NowUserId,
                        CreateUserName = dto.NowUserRealName
                    };
                    Context.TDisasterLevels.Add(disaster);
                    Context.SaveChanges();
                    foreach (var item in dto.material)
                    {
                        item.DisasterLevelId = disaster.Id;
                        Context.TDisasterMaterials.Add(item);
                        Context.SaveChanges();
                    }
                    tran.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    //事务回滚
                    tran.Rollback();
                    throw ex;
                }
            }
            
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool UpdateDisasterLevel(DisasterLevelDto dto)
        {
            //事务
            using (var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    TDisasterLevel disaster = new TDisasterLevel()
                    {
                        Id=dto.Id,
                        Name = dto.Name,
                        Type = dto.Type,
                        Season = dto.Season,
                        Level = dto.Level,
                        Remark = dto.Remark,
                        ModifyDate = DateTime.Now,
                        ModifyUserId = dto.NowUserId,
                        ModifyUserName = dto.NowUserRealName,
                        CreateDate = dto.CreateDate,
                        CreateUserId = dto.NowUserId,
                        CreateUserName = dto.NowUserRealName
                    };
                    Context.TDisasterLevels.Update(disaster);
                    var matlist = Context.TDisasterMaterials.Where(x=>x.DisasterLevelId == dto.Id);
                    if (matlist.Count() > 0)
                    {
                        //删除当前灾情等级的物料信息
                        Context.TDisasterMaterials.RemoveRange(matlist);
                        foreach (var item in dto.material)
                        {
                            item.DisasterLevelId = disaster.Id;
                            Context.TDisasterMaterials.Add(item);
                            
                        }
                    }
                    tran.Commit();
                    return Context.SaveChanges()>0;
                }
                catch (Exception ex)
                {
                    //事务回滚
                    tran.Rollback();
                    throw ex;
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelDisasterLevel(int ids)
        {
            //事务
            using (var tran = Context.Database.BeginTransaction())
            {
                try
                {
                    var matlist = Context.TDisasterMaterials.Where(x => x.DisasterLevelId == ids);
                    if (matlist.Count() > 0)
                    {
                        //删除当前灾情等级的物料信息
                        Context.TDisasterMaterials.RemoveRange(matlist);
                    }
                    var level = Context.TDisasterLevels.Find(ids);
                    Context.TDisasterLevels.Remove(level);
                    tran.Commit();
                    return Context.SaveChanges() > 0;
                }
                catch (Exception ex)
                {
                    //事务回滚
                    tran.Rollback();
                    return false;
                }
            }
        }

        /// <summary>
        /// 灾情等级分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TDisasterLevel> GetDisasterlevels(string name, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TDisasterLevels.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.Name.Contains(name));
                }
                totalCount = list.Count();
                list = list.OrderByDescending(x=>x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                
                return list.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 灾情物料明细分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TDisasterMaterial> GetDisasterMaterials(int ids, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TDisasterMaterials.AsQueryable();
                if (ids!=0)
                {
                    list = list.Where(x => x.DisasterLevelId == ids);
                }
                list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                totalCount = list.Count();
                return list.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public TDisasterLevel Getdisasterlevel(int ids)
        {
            return Context.TDisasterLevels.Find(ids);
        }
    }
}
